var layerLoading;
$(document).ready(function(){
	$("#editBtn").click(showBatchEdit);
	// 取消关注
	$("#cancelFocus").click();
	
	$("#selectAll").click(selectAll);
	
	$(".cart-list-check>span").click(function(){
		var checked = $(this).attr("checked");
		if(checked == "true") {
			$(this).removeClass("cart-list-check-icon-yes").addClass("cart-list-check-icon-no").attr("checked", "false");
		} else {
			$(this).removeClass("cart-list-check-icon-no").addClass("cart-list-check-icon-yes").attr("checked", "true");
		}
	});
	
	$("#addCartBtm").click(add2Cart);
	$("#cancelFavorite").click(cancelFavBatch);
});

/**
 * 取消收藏
 */
function cancelFavBatch() {
	// 获取选中的关注ID
	var ids = [];
	$(".cart-list ul li div .cart-list-check-icon-yes").each(function(){
		ids.push($(this).parent().parent().attr("favId"));
	});
	$.ajax({type:"POST", url: "/favorite/unfavBatch.html",
		data: {"ids": ids.join(",")},
		dataType: "json", 
		beforeSend : function() {
			layerLoading = gshopLayer.loading("正在取消…");
		}, success: function(data) {
			if(data && data > 0) {
				$(".cart-list-check-icon-yes").each(function(){
					$(this).parent().parent().remove();
				});
				$("#wareNumber").val($(".cart-list ul li div .cart-list-check-icon-no").length);
				gshopLayer.success("取消关注成功");
			} else if(data && data.code && data.code == 23) {
				window.location.href = "/login.html";
			}
			gshopLayer.fail(data.msg);
		}, error : function() {
			gshopLayer.fail("取消关注失败");
		}, complete : function() {
			// 关闭加载层
			layer.close(layerLoading);
		}
	});
}

/**
 * 加载到购物车按钮点击后触发的方法，考虑和商品详情的加入购物车共用
 */
function add2Cart() {
	$.ajax({type:"POST", url: "/cart/addNumber/"+$(this).attr("wareId")+".html",
		dataType: "json", 
		beforeSend : function() {
			layerLoading = gshopLayer.loading();
		}, success: function(data) {
			if(data && data.code == 1) {
				$("#carNum").html(parseInt($("#carNum").html()) + 1);
				gshopLayer.success();
			} else if(data && data.code && data.code == 23) {
				window.location.href = "/login.html";
			}
			gshopLayer.fail(data.msg);
		}, error : function() {
			gshopLayer.fail();
		}, complete : function() {
			// 关闭加载层
			layer.close(layerLoading);
		}
	});
}

/**
 * 显示批量编辑按钮和底部编辑工具栏
 */
function showBatchEdit() {
	if($(this).attr("process") == "true") {
		$(this).attr("process", "false");
		$(this).html("批量编辑");
		$(".cart-list-check").hide();
		$(".tools-btm").hide();
	} else {
		$(this).attr("process", "true");
		$(this).html("完成");
		$(".cart-list-check").show();
		$(".tools-btm").show();
	}
	
}
/**
 * 全选
 */
function selectAll() {
	var checked = $(this).attr("checked");
	if(checked == "true") {
		$(".cart-list-check>span").removeClass("cart-list-check-icon-yes").addClass("cart-list-check-icon-no").attr("checked", "false");
		$(this).attr("checked", "false");
	} else {
		$(".cart-list-check>span").removeClass("cart-list-check-icon-no").addClass("cart-list-check-icon-yes").attr("checked", "true");
		$(this).attr("checked", "true");
	}
}
